#!/usr/bin/php5
<?php

function CliErrorHandler($errno, $errstr, $errfile, $errline) {  
	fwrite(STDERR,"$errstr in $errfile on $errline\n");  
}
set_error_handler('CliErrorHandler');

chdir(dirname(__FILE__)) or die("Failed to change directory to " . dirname(__FILE__) . "\n");
$cwd = getcwd();

require("./cfg/settings.php");

function start()
{
	global $USER;
	global $GROUP;
	global $cwd;
	echo "Starting Minecraft server.\n";
	system("start-stop-daemon --start --oknodo --user $USER --group $GROUP --name smpadmin-daemon --pidfile smpadmin.pid --startas $cwd/bin/smpadmin-daemon.php --chuid $USER -d $cwd -b -m");
}

function stop()
{
	global $USER;
	global $GROUP;
	global $cwd;
	echo "Stopping Minecraft server.\n";
	system("start-stop-daemon --stop --oknodo --user $USER --pidfile smpadmin.pid --startas $cwd/bin/smpadmin-daemon.php --chuid $USER -d $cwd -s TERM --retry 30");
}

function reload()
{
	global $USER;
	global $GROUP;
	global $cwd;
	echo "Reloading plugins.\n";
	system("start-stop-daemon --stop --oknodo --user $USER --pidfile smpadmin.pid --startas $cwd/bin/smpadmin-daemon.php --chuid $USER -d $cwd -s USR1");
}

function setup()
{

}

function help()
{
	echo "Usage: smpadmin.php OPTIONS
	start		Start the server
	stop		Stop the server
	restart		Restart the server
	force-reload	Alias for restart
	reload		Reload plugins and settings
	enableplugin	Enable a plugins
	disableplugin	Disable a plugin
	setup		Set up init script and binary\n";
}

if($argc < 2) 
{
	help();
	exit(1);
}

switch($argv[1])
{
case "start";
	start();
	break;
case "stop";
	stop();
        break;
case "force-reload";
case "restart";
	stop();
	start();
        break;
case "reload";
	reload();
        break;
case "enableplugin";
	if($argc != 3)
	{
		echo "Usage: smpadmin.php enableplugin pluginname\n";
		exit(1);
	}

	$plugin = $argv[2];

	if(!is_file("./plugins/" . $plugin . ".php"))
	{
		echo "$plugin does not exist.\n";
		exit(1);
	}

	if(is_file("./plugins-enabled/" . $plugin . ".php"))
	{
		echo "$plugin already enabled.\n";
		exit(1);
	}

	#echo dirname("./plugins-enabled/" . $plugin) . "\n";
	if(!is_dir(dirname("./plugins-enabled/" . $plugin)))
		mkdir(dirname("./plugins-enabled/" . $plugin), 0777, true);

	if(!file_put_contents("./plugins-enabled/" . $plugin . ".php", "enabled"))
	{
		echo "Failed to enable plugin $plugin\n";
		exit(1);
	}
	
	echo "Plugin enabled. Don't forgot to reload!\n";
	
	break;
case "disableplugin";
	if($argc != 3)
	{
		echo "Usage: smpadmin.php disableplugin pluginname\n";
		exit(1);
	}

	$plugin = $argv[2];

	if(!is_file("./plugins-enabled/" . $plugin . ".php"))
	{
		echo "$plugin already disabled.\n";
		exit(1);
	}

	if(!unlink("./plugins-enabled/" . $plugin . ".php"))
	{
		echo "Failed to disable plugin $plugin\n";
		exit(1);
	}
	
	echo "Plugin disabled. Don't forgot to reload!\n";

	break;
case "setup";
	system("ln -s $cwd/smpadmin.php /usr/bin/smpadmin");
	system("ln -s $cwd/smpadmin.php /etc/init.d/smpadmin");
	system("ln -s $cwd/smpadmin.php /etc/rc0.d/smpadmin");
	system("ln -s $cwd/smpadmin.php /etc/rc1.d/smpadmin");
	system("ln -s $cwd/smpadmin.php /etc/rc2.d/smpadmin");
	system("ln -s $cwd/smpadmin.php /etc/rc3.d/smpadmin");
	system("ln -s $cwd/smpadmin.php /etc/rc4.d/smpadmin");
	system("ln -s $cwd/smpadmin.php /etc/rc5.d/smpadmin");
	system("ln -s $cwd/smpadmin.php /etc/rc6.d/smpadmin");
	break;
default:
	help();
	exit(1);
}

?>
